import elasticsearch7
from lxml import etree
from lxml.etree import tostring
import requests
import json
from elasticsearch7 import exceptions as es_exceptions
from app.config.secure import ES_URI, USERNAME, PASSWORD, PORT, ES_URI_REMOTE, ES_URI_REMOTE_PORT
from requests.auth import HTTPBasicAuth
import traceback


class CreatLocalIndex(object):
    server = ES_URI
    headers = {'content-type': 'application/json'}
    es = elasticsearch7.Elasticsearch([ES_URI_REMOTE], http_auth=(USERNAME, PASSWORD), port=ES_URI_REMOTE_PORT)

    def createLocalIndex(self, order_number):
        data = {
            "settings": {
                "number_of_shards": 1,
                "number_of_replicas": 0
            },
            "mappings": {
                "properties": {
                    "title": {
                        "type": "text"
                    },
                    "content": {
                        "type": "text",
                        "analyzer": "ik_max_word"
                    },
                    "url": {
                        "type": "keyword"
                    },
                    "author": {
                        "type": "keyword"
                    },
                    "source": {
                        "type": "keyword"
                    },
                    "university": {
                        "type": "keyword"
                    },
                    "issue": {
                        "type": "text"
                    }
                }
            }
        }
        res = requests.put(f'{ES_URI_REMOTE}:{ES_URI_REMOTE_PORT}/{order_number}?pretty', headers=self.headers, auth=(USERNAME, PASSWORD),
                           data=json.dumps(data))
        print(res.text)

    def deleteLocalIndex(self, order_number):
        if not order_number or order_number == '':
            return
        res = requests.delete(ES_URI + f'/{order_number}?pretty', headers=self.headers, auth=(USERNAME, PASSWORD))
        print(res.text)


class CreatLocalIndexRemote(object):
    server = ES_URI
    headers = {'content-type': 'application/json'}
    es = elasticsearch7.Elasticsearch([ES_URI_REMOTE], http_auth=(USERNAME, PASSWORD), port=ES_URI_REMOTE_PORT)

    def createLocalIndex(self, order_number):
        data = {
            "settings": {
                "number_of_shards": 1,
                "number_of_replicas": 0
            },
            "mappings": {
                "properties": {
                    "title": {
                        "type": "text"
                    },
                    "content": {
                        "type": "text",
                        "analyzer": "ik_max_word"
                    },
                    "url": {
                        "type": "keyword"
                    },
                    "author": {
                        "type": "keyword"
                    },
                    "source": {
                        "type": "keyword"
                    },
                    "university": {
                        "type": "keyword"
                    }
                }
            }
        }
        res = requests.put(f'{ES_URI_REMOTE}:{ES_URI_REMOTE_PORT}/{order_number}?pretty', headers=self.headers, auth=(USERNAME, PASSWORD),
                           data=json.dumps(data))
        print(res.text)

    def deleteLocalIndex(self, order_number):
        if not order_number or order_number == '':
            return
        res = requests.delete(f'{ES_URI_REMOTE}:{ES_URI_REMOTE_PORT}/{order_number}?pretty', headers=self.headers, auth=(USERNAME, PASSWORD))
        print(res.text)


if __name__ == '__main__':
    bot = CreatLocalIndex()
    # bot.createLocalIndex('123123441')
    bot.deleteLocalIndex('123123441')
